package com.jvm.d7;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;

/**
 * 描述:
 *
 * @author luzhongfu
 * Created by lucky on 2019-10-14 3:07 PM
 */
public class Foo {
    private void bar(Object o) {
        System.out.println(o);
    }

    private void say(String say){
        System.out.println(say);
    }

    public static void main(String[] args) throws Throwable {
        MethodHandles.Lookup l = MethodHandles.lookup();

        MethodHandles.Lookup lookup = MethodHandles.publicLookup();

        Method m = Foo.class.getDeclaredMethod("bar", Object.class);
        MethodHandle mh0 = l.unreflect(m);
        mh0.invoke(new Foo(),new Object());

        MethodType methodType = MethodType.methodType(void.class, String.class);
        MethodHandle say = l.findVirtual(Foo.class, "say", methodType);
        say.invokeExact(new Foo(),"hello world");

//        MethodType t = MethodType.methodType(void.class, Object.class);
//        MethodHandle mh = l.findVirtual(Foo.class, "bar", t);
//
//        long current = System.currentTimeMillis();
//        for (int i = 1; i <= 2_000_000_000; i++) {
//            if (i % 100_000_000 == 0) {
//                long temp = System.currentTimeMillis();
//                System.out.println(temp - current);
//                current = temp;
//            }
//            mh.invokeExact(new Foo(), new Object());
//        }

    }
}


class Foo2 {
    public static void bar(Object o) {
//        new Exception().printStackTrace();
        System.out.println(o);
    }

    public static void main(String[] args) throws Throwable {
        MethodHandles.Lookup l = MethodHandles.lookup();
        MethodType t = MethodType.methodType(void.class, Object.class);
        MethodHandle mh = l.findStatic(Foo2.class, "bar", t);
        mh.invokeExact(new Object());
    }
}




